/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.IListingDao;
import org.cx1.dao.api.IPhotoDao;
import org.cx1.dao.impl.ListingDao;
import org.cx1.dao.impl.PhotoDao;
import org.cx1.model.Amenity;
import org.cx1.model.Invite;
import org.cx1.model.Listing;
import org.cx1.model.Photo;
import org.cx1.model.Place;
import org.cx1.model.User;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.EmailInvitee;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IAmenityService;
import org.cx1.service.api.IInviteService;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IMockDSService;
import org.cx1.service.api.IPhotoService;
import org.cx1.service.api.IPlaceService;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DAmenityService;
import org.cx1.service.impl.DInviteService;
import org.cx1.service.impl.DListingService;
import org.cx1.service.impl.DPhotoService;
import org.cx1.service.impl.DPlaceService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MAmenityService;
import org.cx1.service.impl.MInviteService;
import org.cx1.service.impl.MListingService;
import org.cx1.service.impl.MPhotoService;
import org.cx1.service.impl.MPlaceService;
import org.cx1.service.impl.MUserService;
import org.cx1.service.impl.MockDSService;
import org.cx1.service.utils.ListingsCreator;
import org.cx1.service.utils.RoomoramaClient;
import org.cx1.web.controller.action.MockDSAction;
import org.cx1.web.utils.SessionUtils;
import org.cx1.web.utils.SitemapGen;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class MockDSController extends AbstractController {
    
    private IMockDSService dsService = new MockDSService();
    private IListingService listingService = new MListingService();
    
    public MockDSController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        boolean loggedin = false;
        String userID = SessionUtils.getUserID(request.getSession());   
        
        if (!StringUtils.isEmpty(userID)) {
            loggedin = true;
            map.put("loggedin", loggedin);
        }
              
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));        
        if (StringUtils.isEmpty(request.getParameter("ref")) && !StringUtils.equalsIgnoreCase(request.getParameter("ref"), "hamday")) {
            action = 0;
        }
        
        switch (action) {
            case MockDSAction.PERSIST: {                     
                if (loggedin) {
                    persistDS(mav, map, request);        
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.LOAD: {        
                if (loggedin) {
                    loadDS(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CLEAR_LISTINGS: {        
                if (loggedin) {
                    //clearListings(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CLEAR_SHORT_TERM_LISTINGS: {        
                if (loggedin) {
                    //clearShortTermListings(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CLEAR_LONG_TERM_LISTINGS: {        
                if (loggedin) {
                    //clearLongTermListings(mav, map, request);    
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CLEAR_ROOMORAMA_LISTINGS: {        
                if (loggedin) {
                    //clearRoomoramaListings(mav, map, request);     
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CREATE_LISTINGS: {        
                if (loggedin) {
                    //createListings(mav, map, request);      
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CREATE_STOCK_PHOTOS: {        
                if (loggedin) {
                    //createStockPhotos(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CLEAR_UNUSED_PHOTOS: {        
                if (loggedin) {
                    //clearUnusedPhotos(mav, map, request);     
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.ROOMORAMA_CREATE_LISTINGS: {        
                if (loggedin) {
                    //roomoramaCreateListings(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.ROOMORAMA_CLEAR_LISTINGS: {        
                if (loggedin) {     
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.CLEAR_INVITES: {        
                if (loggedin) {
                    //clearInvites(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.GEN_SITEMAP: {        
                if (loggedin) {
                    generateSitemap(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.DB_MIGRATE_USERS: {
                if (loggedin) {
                    //migrateUsers(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.DB_MIGRATE_PLACES: {
                if (loggedin) {
                    //migratePlaces(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.DB_MIGRATE_PHOTOS: {
                if (loggedin) {
                    //migratePhotos(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.DB_MIGRATE_LISTINGS: {
                if (loggedin) {
                    //migrateListings(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.DB_MIGRATE_AMENITIES: {
                if (loggedin) {
                    //migrateAmenities(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.DB_MIGRATE_EMAIL_INVITEES: {
                if (loggedin) {
                    //migrateEmailInvitees(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case MockDSAction.UTIL_CLEAN_LISTING_RATES: {
                if (loggedin) {
                    //cleanListingRates(mav, map, request);       
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }               
                
            default: {
                if (loggedin) {     
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
        }        
        mav.addObject("model", map);
        return mav;
    }
    
    private void persistDS(ModelAndView mav, Map map, HttpServletRequest request) {
        String mapID = request.getParameter("mapID");
        String mapName = request.getParameter("mapName");
        ServiceResponse<Boolean> response = dsService.persistDS(Integer.parseInt(mapID), mapName);
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void loadDS(ModelAndView mav, Map map, HttpServletRequest request) {
        String mapID = request.getParameter("mapID");
        String mapName = request.getParameter("mapName");
        ServiceResponse<Boolean> response = dsService.loadDS(Integer.parseInt(mapID), mapName);
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void clearListings(ModelAndView mav, Map map, HttpServletRequest request) {
        //MockDS.setListings( new ArrayList<Listing>());
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void clearShortTermListings(ModelAndView mav, Map map, HttpServletRequest request) {
        //listingService.clearShortTermListings();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void clearLongTermListings(ModelAndView mav, Map map, HttpServletRequest request) {
        //listingService.clearLongTermListings();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void clearRoomoramaListings(ModelAndView mav, Map map, HttpServletRequest request) {
        //listingService.clearRoomoramaListings();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void createListings(ModelAndView mav, Map map, HttpServletRequest request) {
        //ListingsCreator creator = new ListingsCreator();
        //creator.createListings();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void createStockPhotos(ModelAndView mav, Map map, HttpServletRequest request) {
        //ListingsCreator creator = new ListingsCreator();
        //creator.generateStockPhotos();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void clearUnusedPhotos(ModelAndView mav, Map map, HttpServletRequest request) {
        MockDSService service = new MockDSService();
        service.cleanUnusedPhotos();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void roomoramaCreateListings(ModelAndView mav, Map map, HttpServletRequest request) {
        RoomoramaClient client = new RoomoramaClient();
        String limit = request.getParameter("limit");
        String strCityID = request.getParameter("cityID");
        
        int cityID = StringUtils.isEmpty(strCityID) ? -1 : Integer.parseInt(strCityID);
        
        switch(cityID) {
            case 1: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_MAKATI, RoomoramaClient.CITY_NAME_MAKATI, limit);
                break;
            }
            case 2: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_TAGUIG, RoomoramaClient.CITY_NAME_TAGUIG, limit);
                break;
            }
            case 3: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_MANDALUYONG, RoomoramaClient.CITY_NAME_MANDALUYONG, limit);
                break;
            }
            case 4: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_PASIG, RoomoramaClient.CITY_NAME_PASIG, limit);
                break;
            }
            case 5: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_QUEZON_CITY, RoomoramaClient.CITY_NAME_QUEZON_CITY, limit);
                break;
            }
            case 6: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_MANILA, RoomoramaClient.CITY_NAME_MANILA, limit);
                break;
            }
            case 7: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_PASAY, RoomoramaClient.CITY_NAME_PASAY, limit);
                break;
            }
            case 8: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_PARANAQUE, RoomoramaClient.CITY_NAME_PARANAQUE, limit);
                break;
            }
            case 9: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_MUNTINLUPA, RoomoramaClient.CITY_NAME_MUNTINLUPA, limit);
                break;
            }
            case 10: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_SAN_JUAN, RoomoramaClient.CITY_NAME_SAN_JUAN, limit);
                break;
            }
            case 11: {
                client.reflectListings(RoomoramaClient.API_URL_PROPERTIES_CEBU, RoomoramaClient.CITY_NAME_CEBU, limit);
                break;
            }
        }        
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void clearInvites(ModelAndView mav, Map map, HttpServletRequest request) {
        //MockDS.setInvites( new ArrayList<Invite>());
        mav.setView( new RedirectView("mockds.htm") );
    }    
    
    private void generateSitemap(ModelAndView mav, Map map, HttpServletRequest request) {
        SitemapGen sgen = new SitemapGen();
        sgen.generateSitemap();
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void migrateUsers(ModelAndView mav, Map map, HttpServletRequest request) {
        IUserService mUserService = new MUserService();
        DUserService dUserService = new DUserService();
        
        ServiceResponse<List<User>> response1 = mUserService.readAll(0, 0);
        List<User> users = response1.getResponse();
        
        if (users != null && users.size() > 0) {
            for (User user : users) {
                dUserService.createImport(user);
            }
        }        
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void migratePlaces(ModelAndView mav, Map map, HttpServletRequest request) {
        IPlaceService mPlaceService = new MPlaceService();
        DPlaceService dPlaceService = new DPlaceService();
        
        IPhotoDao photoDao = new PhotoDao();
        
        List<Place> places = mPlaceService.readAll(0, 0).getResponse();
        if (places != null && places.size() > 0) {
            for (Place record : places) {
                Place place = mPlaceService.read(record.getId()).getResponse();
                dPlaceService.createImport(place);
                
                List<Photo> photos = place.getPhotos();
                if (photos != null && photos.size() > 0) {
                    for (Photo photo : photos) {
                        photoDao.addPlacePhoto(place.getId(), photo.getId());
                    }                   
                }
            }
        }
        mav.setView( new RedirectView("mockds.htm") );
    }
        
    private void migrateListings(ModelAndView mav, Map map, HttpServletRequest request) {
        IListingDao listingDao = new ListingDao();
        IPhotoDao photoDao = new PhotoDao();
        
        IListingService mListingService = new MListingService();
        DListingService dListingService = new DListingService();
                
        List<Listing> listings = mListingService.readAll(0, 0).getResponse();
        if (listings != null && listings.size() > 0) {
            for (Listing record : listings) {
                Listing listing = mListingService.read(record.getId()).getResponse();
                
                //CX: import
                dListingService.createImport(listing);
                
                List<Photo> photos = listing.getPhotos();
                if (photos != null && photos.size() > 0) {
                    for (Photo photo : photos) {
                        photoDao.addListingPhoto(listing.getId(), photo.getId());                       
                    }                   
                }
                
                List<Place> places = listing.getPlaces();
                if (places != null && places.size() > 0) {
                    for (Place place : places) {
                        listingDao.addPlaceRef(listing.getId(), place.getId(), "");
                    }                   
                }
                
                List<Amenity> amenities = listing.getAmenities();
                if (amenities != null && amenities.size() > 0) {
                    for (Amenity amenity : amenities) {
                        listingDao.addAmenityRef(listing.getId(), amenity.getId());
                    }                   
                }
            }
        }       
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void migrateAmenities(ModelAndView mav, Map map, HttpServletRequest request) {
        IAmenityService mAmenityService = new MAmenityService();
        DAmenityService dAmenityService = new DAmenityService();
        
        ServiceResponse<List<Amenity>> response1 = mAmenityService.readAll(0, 0);
        List<Amenity> amenities = response1.getResponse();
        
        if (amenities != null && amenities.size() > 0) {
            for (Amenity amenity : amenities) {
                dAmenityService.createImport(amenity);
            }
        }        
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void migratePhotos(ModelAndView mav, Map map, HttpServletRequest request) {
        IPhotoService mPhotoService = new MPhotoService();
        DPhotoService dPhotoService = new DPhotoService();
        
        ServiceResponse<List<Photo>> response1 = mPhotoService.readAll(0, 0);
        List<Photo> photos = response1.getResponse();
        
        if (photos != null && photos.size() > 0) {
            for (Photo photo : photos) {
                dPhotoService.createImport(photo);
            }
        }        
        mav.setView( new RedirectView("mockds.htm") );
    }
    
    private void migrateEmailInvitees(ModelAndView mav, Map map, HttpServletRequest request) {
        IInviteService mInviteService = new MInviteService();
        IInviteService dInviteService = new DInviteService();
        
        ServiceResponse<List<EmailInvitee>> response1 = mInviteService.readAllEmailInvitee(0, 0);
        List<EmailInvitee> emailInvitees = response1.getResponse();
        
        if (emailInvitees != null && emailInvitees.size() > 0) {
            for (EmailInvitee emailInvitee : emailInvitees) {
                dInviteService.createEmailInvitee(emailInvitee);
            }
        }        
        mav.setView( new RedirectView("mockds.htm") );
    }
        
    private void cleanListingRates(ModelAndView mav, Map map, HttpServletRequest request) {
        IListingService listingService = new DListingService();
        List<Listing> listings = listingService.readAll(0, 0).getResponse();
        
        for (Listing listing : listings) {
            float longMonthRateNum = 0;
            float shortMonthRateNum = 0;
            float shortWeekRateNum = 0;
            float shortDayRateNum = 0;
            
            String strLongMonthRate = listing.getLongMonthRate();
            String strShortMonthRate = listing.getShortMonthRate();
            String strShortWeekRate = listing.getShortWeekRate();
            String strShortDayRate = listing.getShortDayRate();
            
            try {                
                if (!StringUtils.isEmpty(strLongMonthRate)) {
                    strLongMonthRate = strLongMonthRate.trim();
                    strLongMonthRate = strLongMonthRate.replaceAll("(?<=^\\d+)\\.0*$", "");
                    strLongMonthRate = strLongMonthRate.replaceAll(",", "");
                    longMonthRateNum = Float.parseFloat(strLongMonthRate);
                    
                    strShortMonthRate = strShortMonthRate.trim();
                    strShortMonthRate = strShortMonthRate.replaceAll("(?<=^\\d+)\\.0*$", "");
                    strShortMonthRate = strShortMonthRate.replaceAll(",", "");
                    shortMonthRateNum = Float.parseFloat(strShortMonthRate);
                    
                    strShortWeekRate = strShortWeekRate.trim();
                    strShortWeekRate = strShortWeekRate.replaceAll("(?<=^\\d+)\\.0*$", "");
                    strShortWeekRate = strShortWeekRate.replaceAll(",", "");
                    shortWeekRateNum = Float.parseFloat(strShortWeekRate);
                    
                    strShortDayRate = strShortDayRate.trim();
                    strShortDayRate = strShortDayRate.replaceAll("(?<=^\\d+)\\.0*$", "");
                    strShortDayRate = strShortDayRate.replaceAll(",", "");
                    shortDayRateNum = Float.parseFloat(strShortDayRate);
                }
            } 
            catch (Exception ex) {
                
            }
            
            listing.setLongMonthRateNum(longMonthRateNum);
            listing.setShortMonthRateNum(shortMonthRateNum);
            listing.setShortWeekRateNum(shortWeekRateNum);
            listing.setShortDayRateNum(shortDayRateNum);
            
            listingService.update(listing);            
        }
        mav.setView( new RedirectView("mockds.htm") );
    }
    
}
